package com.jit.BluetoothKey.activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.jit.BluetoothKey.R;
import com.jit.BluetoothKey.activity.base.BaseActivity;
import com.jit.BluetoothKey.utils.SharedPreferencesUtil;
import com.jit.BluetoothKey.utils.UserFiled;
import com.jit.BluetoothKey.view.MyDialog;

import org.greenrobot.eventbus.EventBus;

public class AccountActivity extends BaseActivity implements View.OnClickListener {

    private TextView pinCode;
    private Button modificationButton;
    private Button logout;
    private TextView user_name_tv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_account);

    }


    @Override
    protected void initView() {
        super.initView();
        pinCode = (TextView) findViewById(R.id.account_pin_code_name_tv);
        modificationButton = (Button) findViewById(R.id.account_modification_btn);
        logout = (Button) findViewById(R.id.account_logout_btn);
        user_name_tv = (TextView) findViewById(R.id.account_user_name_tv);
    }


    @Override
    protected void initData() {
        super.initData();
        user_name_tv.setText(SharedPreferencesUtil.getString(this,UserFiled.ACCOUNT));
        pinCodeSet();
    }

    private void pinCodeSet() {
        String code = SharedPreferencesUtil.getString(this, UserFiled.SK);
        pinCode.setText(code);
    }


    @Override
    protected void initAdapt() {
        super.initAdapt();
    }

    @Override
    protected void initListner() {
        super.initListner();
        modificationButton.setOnClickListener(this);
        logout.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.account_modification_btn:
                // TODO: 2018/2/28  点击修改注册码
                modificationPincodeDiolog();
                break;
            case R.id.account_logout_btn:
                // TODO: 2018/3/6  一键退出 跳转到登录页
                userExit();
                break;
        }
    }

    private void userExit() {
        EventBus.getDefault().post(UserFiled.EXIT);
        SharedPreferencesUtil.setIsLogin(this,UserFiled.IS_LOGIN,false);
        finish();
        startActivity(new Intent(this,LoginActivity.class));
    }

    private void modificationPincodeDiolog() {
        MyDialog myDialog = new MyDialog(this);
        myDialog.setPinCodeChangeListner(new MyDialog.PinCodeChangeListner() {
            @Override
            public void pinCodeChange() {
                pinCodeSet();
            }
        });
        Dialog init = myDialog.init();
        init.show();
    }
}
